package com.techyuan.algorithm.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by tianjuntao on 2016/12/8.
 */
public class TowSum {
    /**
     * 思路：使用hashmap来存储之前数组位置的补余值。
     * <p/>
     * <p/>
     * int[] nums = new int[]{2, 4, 5, 9};
     * PrintUtil.printArrPretty(nums);
     * PrintUtil.printArrPretty(twoSum(nums, 9));
     *
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                return new int[]{map.get(nums[i]), i};
            }
            map.put(target - nums[i], i);
        }

        return new int[]{0, 0};
    }



}
